package com.shrct.businessprocess.product.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.product.RCDerivedFullServiceProductSpecification;
import com.shrct.businessprocess.product.RCNormalizerService;
import com.shrct.businessprocess.product.RCProductMetaType;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCProductSpecificationDAO;
import com.shrct.businessprocess.product.RCServiceInfo;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.product.events.RCProductSpecificationEventsManager;
import com.shrct.businessprocess.product.impl.RCDerivedFullServiceProductSpecificationImpl;
import com.shrct.businessprocess.product.impl.RCProductSpecificationImpl;
import com.uniquesoft.uidl.validation.Validates;
import edu.fudan.mylang.common.ObjectExtensions2;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.product.RCProductSpecificationDAO")
public class RCProductSpecificationDAOImpl extends BaseService implements RCProductSpecificationDAO {
  @Autowired
  private RCProductSpecificationEventsManager eventsManager;
  
  @Autowired
  private RCNormalizerService normalizerService;
  
  public RCProductSpecification findByCode(final String code) {
    IGenericQuery<RCProductSpecification> query=getObjectFactory().createGenericQuery(RCProductSpecification.class,"select p from com.shrct.businessprocess.product.impl.RCProductSpecificationImpl as p   where ((p.code) = (:code)) and  p.active = 1      ").setParameter("code",code);
    RCProductSpecification _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public RCDerivedFullServiceProductSpecification getFullService(final RCProductSpecification productSpec, final String serviceSpec, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Integer months) {
    RCDerivedFullServiceProductSpecification _xblockexpression = null;
    {
      String _xifexpression = null;
      boolean _equals = Objects.equal(serviceLevel, null);
      if (_equals) {
        _xifexpression = "";
      } else {
        String _codeid = serviceLevel.getCodeid();
        _xifexpression = _codeid;
      }
      final String sl = _xifexpression;
      String _xifexpression_1 = null;
      boolean _equals_1 = Objects.equal(months, null);
      if (_equals_1) {
        _xifexpression_1 = "";
      } else {
        String _string = months.toString();
        _xifexpression_1 = _string;
      }
      final String m = _xifexpression_1;
      String _xifexpression_2 = null;
      boolean _equals_2 = Objects.equal(serviceResponseTime, null);
      if (_equals_2) {
        _xifexpression_2 = "";
      } else {
        String _codeid_1 = serviceResponseTime.getCodeid();
        _xifexpression_2 = _codeid_1;
      }
      final String sr = _xifexpression_2;
      String _code = productSpec.getCode();
      String _plus = (_code + ":");
      String _plus_1 = (_plus + serviceSpec);
      String _plus_2 = (_plus_1 + ":");
      String _plus_3 = (_plus_2 + sl);
      String _plus_4 = (_plus_3 + ":");
      String _plus_5 = (_plus_4 + sr);
      String _plus_6 = (_plus_5 + ":");
      final String code = (_plus_6 + m);
      IGenericQuery<RCDerivedFullServiceProductSpecification> query=getObjectFactory().createGenericQuery(RCDerivedFullServiceProductSpecification.class,"select s from com.shrct.businessprocess.product.impl.RCDerivedFullServiceProductSpecificationImpl as s   where ((s.code) = (:code)) and  s.active = 1      ").setParameter("code",code);
      final RCDerivedFullServiceProductSpecification r = query.uniqueResult();
      RCDerivedFullServiceProductSpecification _xifexpression_3 = null;
      boolean _notEquals = (!Objects.equal(r, null));
      if (_notEquals) {
        _xifexpression_3 = r;
      } else {RCDerivedFullServiceProductSpecification rCDerivedFullServiceProductSpecification=RCDerivedFullServiceProductSpecificationImpl.create(productSpec,getObjectFactory());
        final Procedure1<RCDerivedFullServiceProductSpecification> _function = new Procedure1<RCDerivedFullServiceProductSpecification>() {
            public void apply(final RCDerivedFullServiceProductSpecification it) {
              it.setCode(code);
              RCServiceInfo _rCServiceInfo = new RCServiceInfo();
              final Procedure1<RCServiceInfo> _function = new Procedure1<RCServiceInfo>() {
                  public void apply(final RCServiceInfo it) {
                    it.setServiceLevel(serviceLevel);
                    it.setServiceResponseTime(serviceResponseTime);
                    it.setMonthes(months);
                  }
                };
              RCServiceInfo _doubleArrow = ObjectExtensions.<RCServiceInfo>operator_doubleArrow(_rCServiceInfo, _function);
              it.setServiceType(_doubleArrow);
            }
          };
        RCDerivedFullServiceProductSpecification _doubleArrow = ObjectExtensions.<RCDerivedFullServiceProductSpecification>operator_doubleArrow(rCDerivedFullServiceProductSpecification, _function);
        _xifexpression_3 = _doubleArrow;
      }
      _xblockexpression = (_xifexpression_3);
    }
    return _xblockexpression;
  }
  
  public RCProductSpecification create(final String _code, final Procedure1<? super RCProductSpecification> postInit) {
    RCProductSpecification _xblockexpression = null;
    {
      final String code = _code.toUpperCase();
      boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(code);
      boolean _not = (!_isNullOrEmpty);
      Validates.Assert(_not, "\u4EA7\u54C1\u578B\u53F7\u4E0D\u80FD\u4E3A\u7A7A");
      RCProductSpecification _findByCode = this.findByCode(code);
      boolean _equals = Objects.equal(_findByCode, null);
      String _plus = ("\u4EA7\u54C1\u578B\u53F7\u5DF2\u5B58\u5728:" + code);
      Validates.Assert(_equals, _plus);RCProductSpecification rCProductSpecification=RCProductSpecificationImpl.create(getObjectFactory());
      final Procedure1<RCProductSpecification> _function = new Procedure1<RCProductSpecification>() {
          public void apply(final RCProductSpecification p) {
            postInit.apply(p);
            p.setCode(code);
            String _normalizeSid = RCProductSpecificationDAOImpl.this.normalizerService.normalizeSid(code);
            p.setNormalizedCode(_normalizeSid);
            RCProductSpecificationDAOImpl.this.eventsManager.fireCreated(p);
          }
        };
      RCProductSpecification _doubleArrow = ObjectExtensions.<RCProductSpecification>operator_doubleArrow(rCProductSpecification, _function);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
  
  public RCProductSpecification createOrFind(final String _code, final RCProductMetaType productMetaType) {
    RCProductSpecification _xblockexpression = null;
    {
      final String code = _code.toUpperCase();
      RCProductSpecification _findByCode = this.findByCode(code);
      final Function1<Void,RCProductSpecification> _function = new Function1<Void,RCProductSpecification>() {
          public RCProductSpecification apply(final Void it) {
            final Procedure1<RCProductSpecification> _function = new Procedure1<RCProductSpecification>() {
                public void apply(final RCProductSpecification it) {
                  it.setProductMetaType(productMetaType);
                }
              };
            RCProductSpecification _create = RCProductSpecificationDAOImpl.this.create(code, _function);
            return _create;
          }
        };
      RCProductSpecification _withDefault = ObjectExtensions2.<RCProductSpecification>withDefault(_findByCode, _function);
      _xblockexpression = (_withDefault);
    }
    return _xblockexpression;
  }
  
  public void delete(final RCProductSpecification e, final Procedure1<? super RCProductSpecification> preDelete) {
    preDelete.apply(e);getObjectFactory().delete(e);
    
    this.eventsManager.fireDeleted(e);
  }
}
